from tkinter.scrolledtext import ScrolledText
from tkinter import Button, Radiobutton, Tk, END, IntVar
from tkinter import *
import threading
import index, crawl, pictures, store
import queue
import time

q = queue.Queue()  # 控制爬去的数据的顺序
flag = True
one = None
two = None
three = None
wc = None


def func():
    pass


def producer(i, t):  # 生产者线程，负责爬取数据添加
    q.put(crawl.get_data(i, t))


def customer():  # 消费者线程，负责将爬到的数据写入文本框
    try:
        data = q.get()
        st.insert(END, '%s\t%s\t%d\t%d\t%d\t%d\n' % (
            data['type'], data['gdm'], data['gqs'], data['pls'], data['bfl'], data['scs']))
        # 存入数据库
        store.saveinMySQL(data)
        st.see(END)
    except:
        pass


def gocrawl():  # 准备爬取，判断爬取的类别
    global flag
    flag = True
    if type.get() == 0:
        thread_it(startcrawl, '华语')
    elif type.get() == 1:
        thread_it(startcrawl, '欧美')
    elif type.get() == 2:
        thread_it(startcrawl, '日语')
    elif type.get() == 3:
        thread_it(startcrawl, '韩语')
    elif type.get() == 4:
        thread_it(startcrawl, '粤语')


def startcrawl(t):  # 开始爬取
    # 记录爬取时间
    starttime = time.time()
    sum = 0
    st.insert(END, '正在爬取%s歌单\n' % t)
    st.see(END)
    for count in range(38):
        # 判断是否已经停止爬取
        if flag == False:
            break
        # 按页爬取歌单url
        index.get_new_urls(
            'https://music.163.com/discover/playlist/?&cat={0}&order=hot&limit=35&offset={1}'.format(t, 35 * count))
        for i in index.a_urls:
            time.sleep(0.1)
            sum += 1
            index.old_urls.append(i)
            # 调用生产者线程
            t1 = threading.Thread(target=producer, args=(i, t))
            t1.start()
            index.a_urls.remove(i)
            # 调用消费者线程
            t2 = threading.Thread(target=customer)
            t2.start()
        q.empty()
        time.sleep(0.1)

    time.sleep(5)
    st.insert(END, '共爬取%d条数据，用时%.2f秒\n' % (sum - 1, time.time() - starttime - 5))
    st.see(END)


def stopcrawl():  # 停止爬取
    global flag
    # 控制全局变量flag在startcrawl()中判断是否继续爬取
    flag = False
    st.insert(END, '正在停止爬取，请稍等\n')
    st.see(END)


def thread_it(func, *args):
    t = threading.Thread(target=func, args=args)
    t.setDaemon(True)
    t.start()


def picone():  # 饼状图
    global one
    pictures.one()
    one = PhotoImage(file='pic/one.png')
    st.image_create(END, image=one)
    st.see(END)


def pictwo():  # 散点图及其拟合直线
    global two
    pictures.two()
    two = PhotoImage(file='pic/two.png')
    st.image_create(END, image=two)
    st.see(END)


def picthr():  # 箱线图
    global three
    pictures.three()
    three = PhotoImage(file='pic/three.png')
    st.image_create(END, image=three)
    st.see(END)


def picwc():  # 词云图
    global wc
    pictures.wc()
    wc = PhotoImage(file='pic/wc.png')
    st.image_create(END, image=wc)
    st.see(END)


root = Tk()
# 设置标题图标
root.title('网易云音乐歌单爬虫')
root.iconbitmap('pic/music.ico')
# 滚动文本框
st = ScrolledText(root)
st.grid(row=0, column=0, columnspan=5, rowspan=4)
# radio及其布局
type = IntVar()
type.set(0)
ch1 = Radiobutton(root, text='华语', variable=type, value=0)
ch2 = Radiobutton(root, text='欧美', variable=type, value=1)
ch3 = Radiobutton(root, text='日语', variable=type, value=2)
ch4 = Radiobutton(root, text='韩语', variable=type, value=3)
ch5 = Radiobutton(root, text='粤语', variable=type, value=4)
ch1.grid(row=4, column=0)
ch2.grid(row=4, column=1)
ch3.grid(row=4, column=2)
ch4.grid(row=4, column=3)
ch5.grid(row=4, column=4)
# button及其布局
btn1 = Button(root, text='开始爬取', command=gocrawl)
btn2 = Button(root, text='停止爬取', command=stopcrawl)
btn3 = Button(root, text='图1', command=picone)
btn4 = Button(root, text='图2', command=pictwo)
btn5 = Button(root, text='图3', command=picthr)
btn6 = Button(root, text='词云图', command=picwc)
btn1.grid(row=5, column=1)
btn2.grid(row=5, column=3)
btn3.grid(row=0, column=5)
btn4.grid(row=1, column=5)
btn5.grid(row=2, column=5)
btn6.grid(row=3, column=5)
root.mainloop()
